<?php
/* Form to edit an event */

include('includes/functions.php');
$event = get_event($_GET['day'],$_GET['month'],$_GET['year'],$_GET['slug']);
$title = "Edit " . $event['title'];
include('includes/header.php');
if ($event['user'] != $_SESSION['UserID'] && !is_admin($_SESSION['UserID'])) {
	header("Location: ../");
}

if ($_POST) {
	$startdate = $_POST['syear'] . "-" . $_POST['smonth'] . "-" . $_POST['sday'];
	
	$shour = $_POST['shour'];
	if ($_POST['sampm'] == 1) $shour = $shour + 12;
	$ehour = $_POST['ehour'];
	if ($_POST['eampm'] == 1) $ehour = $ehour + 12;
	
	if ($_POST['allday'] == 0) {
		$stime = $shour . ":" . $_POST['smin'];
		$etime = $ehour . ":" . $_POST['emin'];
		$allday = 0;
		$eday = $_POST['eday'];
	} else {
		$allday = 1;
		$stime = "00:00:00";
		$etime = "00:00:00";
		$eday = $_POST['eday'] + 1;
	}
	
	$enddate = $_POST['eyear'] . "-" . $_POST['emonth'] . "-" . $eday;
	$user = get_user($_SESSION['UserID']);
	$apv = $user['approved'];
	
	if (edit_event($event['id'], $_POST['title'],$_SESSION['UserID'],null,$allday,$startdate,$stime,$enddate,$etime,$_POST['desc'], $apv)) {
		if ($_POST['new_meta_type']) {
			foreach ($_POST['new_meta_type'] as $key => $meta_type) {
				if (strlen($_POST['new_meta_value'][$key])>0) {
					add_meta($id,$meta_type,$_POST['new_meta_value'][$key]);
				}
			}
		}
		if ($_POST['meta_value']) {
			foreach ($_POST['meta_value'] as $key => $meta_value) {
				if (strlen($meta_value)>0) {
					update_meta($key,$meta_value);
				} else {
					remove_meta($key);
				}
			}
		}
		echo "<div class='good-news'>";
		if ($apv == 1) echo "Event Updated.";
		else echo "Event Awaiting Moderation.";
		echo "</div>";
	} 
$event = get_event($_GET['day'],$_GET['month'],$_GET['year'],$_GET['slug']); //get updated info
}

$sdate = explode_date($event['startdate']);
$edate = explode_date($event['enddate']);
$stime = explode_time($event['starttime']);
$etime = explode_time($event['endtime']);

?>
<script>
	function toggle_time() {
		if (document.getElementById('allday[1]').checked) {
			document.getElementById('stime').style.visibility = 'hidden';
			document.getElementById('etime').style.visibility = 'hidden';
		} else {
			document.getElementById('stime').style.visibility = 'visible';
			document.getElementById('etime').style.visibility = 'visible';
		}
	}
</script>
<?php 	
$metas = get_meta_keys();
$i=0;
?>
<script>
	function addMetaElement() {
	  var ni = document.getElementById('MetaDiv');
	  var num = (document.getElementById('MetaStartValue').value -1)+ 1;
		document.getElementById('MetaStartValue').value = parseInt(document.getElementById('MetaStartValue').value) + 1;
	  var newdiv = document.createElement('div');
	  var divIdName = 'my'+num+'Div';
	  var contents;
	  newdiv.setAttribute('id',divIdName);
	  contents = "	<div class='form-row'>\n\
		<div class='form-label'>\n\
				<select name='new_meta_type["+num+"]'>\n\
					<?php foreach ($metas as $ID => $meta) { ?>\n\
						<option value='<?php echo $ID; ?>'";
						if (num+1 == '<?php echo $ID; ?>') {
						contents = contents + " SELECTED='SELECTED'";
						}
						contents = contents + "><?php echo $meta['desc']; ?></option>\n\
					<?php } ?>\n\
				</select>\n\
		</div>\n\
		<div class='form-field'><input type='text' name='new_meta_value["+num+"]' id='nw_meta_value["+num+"]' size='50' /></div>\n\
	</div>";
	  newdiv.innerHTML = contents;
	  ni.appendChild(newdiv);
	}
</script>
<form name="edit" id="edit" method="post" >
	<div class="form-row">
		<div class="form-label">
			<label for="title">Title: </label>
		</div>
		<div class="form-field">
			<input type="text" name="title" id="title" value="<?php echo $event['title']; ?>"  size="50"/>
		</div>
	</div>
	<div class="form-row">
		<div class="form-label">
			<label for="allday">All Day Event: </label>
		</div>
		<div class="form-field">
			<input type="radio" name="allday" id="allday[0]" <?php if (!$event['allday']) echo  'checked="true"'; ?> onChange='toggle_time()'/><label for="allday[0]">No</label><br />
			<input type="radio" name="allday" id="allday[1]" <?php if ($event['allday']) echo  'checked="true"'; ?> onChange='toggle_time()'/><label for="allday[1]">Yes</label><br />
		</div>
	</div>
	<div class="form-row">
		<div class="form-label">
			<label for="sday">Start Date: </label>
		</div>
		<div class="form-field">
			<select name="smonth" id="smonth" onchange="document.getElementById('emonth').value=this.value" >
			<?php for ($i = 1; $i <= 12; $i++) { ?>
				<option value="<?php echo make_two($i); ?>" <?php if ($sdate['month'] == make_two($i)) echo "selected='selected'"; ?>><?php echo $i; ?></option>
			<?php } ?>
			</select>
			 / 
			<select name="sday" id="sday" onchange="document.getElementById('eday').value=this.value" >
			<?php for ($i = 1; $i <= 31; $i++) { ?>
				<option value="<?php echo make_two($i); ?>" <?php if ($sdate['day'] == make_two($i)) echo "selected='selected'"; ?>><?php echo $i; ?></option>
			<?php } ?>
			</select>
			 / 
			<select name="syear" id="syear" onchange="document.getElementById('eyear').value=this.value">
<?php for ($i = date('Y'); $i <= date('Y') + 1; $i++) { ?>
				<option value="<?php echo $i; ?>" <?php if ($sdate['year'] == make_two($i)) echo "selected='selected'"; ?>><?php echo $i; ?></option>
			<?php } ?>

			</select>
		</div>
	</div>
	<div class="form-row" id='stime'>
		<div class="form-label">
			<label for="shour">Start Time: </label>
		</div>
		<div class="form-field">
			<select name="shour" id="shour" onchange="document.getElementById('ehour').value=this.value" >
			<?php for ($i = 1; $i <= 12; $i++) { ?>
				<option value="<?php echo make_two($i); ?>" <?php if ($stime['hour'] == make_two($i)) echo "selected='selected'"; ?>><?php echo $i; ?></option>
			<?php } ?>
			</select>
			 : 
			<select name="smin" id="smin" onchange="document.getElementById('emin').value=this.value">
			<?php for ($i = 0; $i <= 45; $i = $i + 15) { ?>
				<option value="<?php echo make_two($i); ?>" <?php if ($stime['min'] == make_two($i)) echo "selected='selected'"; ?>><?php echo make_two($i); ?></option>
			<?php } ?>
			</select>
			 &nbsp; 
			<select name="sampm" id="sampm" onchange="document.getElementById('eampm').value=this.value">
				<option value="0" <?php if ($stime['ampm'] == 0) echo "selected='selected'"; ?>>AM</option>
				<option value="1" <?php if ($stime['ampm'] == 1) echo "selected='selected'"; ?>>PM</option>
			</select>
		</div>
	</div>
	<div class="form-row">
		<div class="form-label">
			<label for="eday">End Date: </label>
		</div>
		<div class="form-field">
			<select name="emonth" id="emonth">
			<?php for ($i = 1; $i <= 12; $i++) { ?>
				<option value="<?php echo make_two($i); ?>" <?php if ($edate['month'] == make_two($i)) echo "selected='selected'"; ?>><?php echo $i; ?></option>
			<?php } ?>
			</select>
			 / 
			<select name="eday" id="eday">
			<?php for ($i = 1; $i <= 31; $i++) { ?>
				<option value="<?php echo make_two($i); ?>" <?php
				if ($event['allday']) {
					if ($edate['day'] - 1 == make_two($i)) echo "selected='selected'"; 
				} else { 
					if ($edate['day'] == make_two($i)) echo "selected='selected'"; 
				}
				?>><?php echo $i; ?></option>
			<?php } ?>
			</select>
			 / 
			<select name="eyear" id="eyear" >
<?php for ($i = date('Y'); $i <= date('Y') + 1; $i++) { ?>
				<option value="<?php echo $i; ?>" <?php if ($edate['year'] == make_two($i)) echo "selected='selected'"; ?>><?php echo $i; ?></option>
			<?php } ?>
			</select>
		</div>
	</div>
	<div class="form-row" id='etime'>
		<div class="form-label">
			<label for="shour">End Time: </label>
		</div>
		<div class="form-field">
			<select name="ehour" id="ehour">
			<?php for ($i = 1; $i <= 12; $i++) { ?>
				<option value="<?php echo make_two($i); ?>" <?php if ($etime['hour'] == make_two($i)) echo "selected='selected'"; ?>><?php echo $i; ?></option>
			<?php } ?>
			</select>
			 : 
			<select name="emin" id="emin">
			<?php for ($i = 0; $i <= 45; $i = $i + 15) { ?>
				<option value="<?php echo make_two($i); ?>" <?php if ($etime['min'] == make_two($i)) echo "selected='selected'"; ?>><?php echo make_two($i); ?></option>
			<?php } ?>
			</select>
			 &nbsp; 
			<select name="eampm" id="eampm">
				<option value="0" <?php if ($etime['ampm'] == 0) echo "selected='selected'"; ?>>AM</option>
				<option value="1" <?php if ($etime['ampm'] == 1) echo "selected='selected'"; ?>>PM</option>
			</select>
		</div>
	</div>
	<div class="form-row">
		<div class="form-label">
			<label for="desc">Description: </label>
		</div>
		<div class="form-field">
			<textarea name="desc" id="desc" rows="7" cols="36"><?php echo $event['desc']; ?></textarea>
		</div>
	</div>
<?php  
	$metas = get_meta_keys();
	foreach (get_meta($event['id']) as $key=>$meta) { ?> 
	<div class='form-row'>
		<div class='form-label'>
				<?php echo $metas[$meta['key']]['desc']; ?>:
		</div>
		<div class='form-field'><input type='text' name='meta_value[<?php echo $key; ?>]' id='meta_value[<?php echo $key; ?>]' size='50' value='<?php echo $meta['value']; ?>'/></div>
	</div>
	<?php } ?>
	<input type="hidden" value="0" id="MetaStartValue" />
	<div id="MetaDiv"> </div>
	<div class='form-row'>
		<div class='form-label'>
		 &nbsp;
		</div>
		<div class='form-field'>
			<input type='submit' value='Update Event'> [ <a href="javascript:;" onclick="addMetaElement();">Add Another Field</a> ]
		</div>
	</div>
</form>
<div class='clear'> </div>
</form>

<?php
if ($event['allday']) { ?>
<script>
toggle_time();
</script>
<?php } ?>
<div class='clear'> </div>
<?php 
include('includes/footer.php');
?>